package com.jinzhi.jzweb.controller.moduleConfig;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.moduleConfig.DecryptDO;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptNum1Dto;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptNum2Dto;
import com.jinzhi.jzweb.pojo.moduleConfig.OverallStatisticsVo;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.ExaminationResultsService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.service.moduleConfig.DecryptService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.domain.UserDO;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/*
 *  @author : JingChangTian
 *  @date   : 2024/3/18 14:18
 *  @description : 赛场得分统计
 *
 */
@Controller
@Api(value = "解密管理", tags = {"解密管理"})
public class DecryptController extends BaseController {

    private final DecryptService decryptService;

    private final StuSignUpComService stuSignUpComService;

    private final ExaminationResultsComService examinationResultsService;

    public DecryptController(DecryptService decryptService, StuSignUpComService stuSignUpComService,ExaminationResultsComService examinationResultsService) {
        this.decryptService = decryptService;
        this.stuSignUpComService = stuSignUpComService;
        this.examinationResultsService = examinationResultsService;
    }





    @ApiOperation(value = "导入一次加密信息", notes = "导入一次加密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MultipartFile", value = "文件表格", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "String", value = "赛场", required = true, dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/importExcelDecryptNum1")
    public Result<Object> importExcelDecryptNum1(@RequestParam("file") MultipartFile file, String field){
        return decryptService.importExcelDecryptNum1(file,field);
    }

    @ApiOperation(value = "导入二次加密信息", notes = "导入二次加密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MultipartFile", value = "文件表格", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "String", value = "赛场", required = true, dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/importExcelDecryptNum2")
    public Result<Object> importExcelDecryptNum2(@RequestParam("file") MultipartFile file, String field){
        return decryptService.importExcelDecryptNum2(file,field);
    }


    @ApiOperation(value = "修改解密信息", notes = "修改解密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decryptDO", value = "解密对象", required = true, dataType = "DecryptDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateDecrypt")
    public Result<Object> updateDecrypt(@RequestBody DecryptDO decryptDO){
        return decryptService.updateDecrypt(decryptDO);
    }

    @ApiOperation(value = "修改解密信息", notes = "修改解密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decryptDO", value = "解密对象", required = true, dataType = "DecryptDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateDecryptMatch")
    public Result<Object> updateDecryptMatch(@RequestBody DecryptDO decryptDO){
        return decryptService.updateDecryptMatch(decryptDO);
    }


    @ApiOperation(value = "根据赛场查询已经匹配解密列表", notes = "根据赛场查询已经匹配解密列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decryptDO", value = "解密对象", required = true, dataType = "DecryptDO"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getDecrypt")
    public Result<Object> getDecrypt(@RequestBody DecryptDO decryptDO){
        return decryptService.getDecrypt(decryptDO);
    }



    @ApiOperation(value = "一次二次加密绑定下拉框", notes = "一次二次加密绑定下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "field", value = "赛场", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/getBindNum")
    public Result<Object> getBindNum(String field,String status){
        return decryptService.getBindNum(field,status);
    }



    @ApiOperation(value = "根据赛场查询未匹配解密列表", notes = "根据赛场查询未匹配解密列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decryptDO", value = "解密对象", required = true, dataType = "DecryptDO"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getAddDecrypt")
    public Result<Object> getAddDecrypt(@RequestBody DecryptDO decryptDO){
        return decryptService.getAddDecrypt(decryptDO);
    }




    @ApiOperation(value = " 导出根据赛场,类型导出解密信息", notes = " 导出根据赛场,类型导出解密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "field", value = "赛场信息ABCD", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型1:一次加密，2:二次加密3:一二次加密", required = true, dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/downloadImportDecrypt")
    public void downloadImportDecrypt(HttpServletResponse response, String field, Integer type){
        decryptService.downloadImportDecrypt(response,field,type);
    }


    @ApiOperation(value = "根据id删除解密信息", notes = "根据id删除解密信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前跳id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/deltedDecrypt")
    public Result<Object> deltedDecrypt(Long id,String status){
        return decryptService.deltedDecrypt(id,status);
    }


    @ApiOperation(value = "根据真实考生id绑定考生一次二次加密", notes = "根据真实考生id绑定考生一次二次加密")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考生id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "num1s", value = "一次加密", required = true, dataType = "String"),
            @ApiImplicitParam(name = "field", value = "赛场", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/updateStuCopy")
    public Result<Object> updateStuCopy(Long id,String num1s,String field){
        return decryptService.updateStuCopy(id,num1s, field);
    }




    @ApiOperation(value = "一次加密导入模版", notes = "一次加密导入模版")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/exportListNum1")
    @ResponseBody
    public void exportListNum1(HttpServletRequest request, HttpServletResponse respons) throws Exception{
        List<DecryptNum1Dto> list = new LinkedList<>();
        //导出操作
        EasyPoiUtil.exportExcel(list, "赛道绑定导入模版", "赛道绑定导入模版", DecryptNum1Dto.class, "赛道绑定导入模版", respons);
    }


    @ApiOperation(value = "二次加密导入模版", notes = "二次加密导入模版")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/exportListNum2")
    @ResponseBody
    public void exportListNum2(HttpServletRequest request, HttpServletResponse respons) throws Exception{
        List<DecryptNum2Dto> list = new LinkedList<>();
        //导出操作
        EasyPoiUtil.exportExcel(list, "赛道绑定导入模版", "赛道绑定导入模版", DecryptNum2Dto.class, "赛道绑定导入模版", respons);
    }

    @ApiOperation(value = "获取理论虚拟二次加密", notes = "获取理论虚拟二次加密")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/getNum2")
    @ResponseBody
    public Result getNum2(String field){
        Integer type = 0;
        if(field.equals("A")){
            type = 1;
        }else if (field.equals("C")){
            type = 2;
        }
        List<DecryptDO> decryptDOS = new ArrayList<>();
        List<StuSignUpComDO> doList = stuSignUpComService.selectList(new EntityWrapper<StuSignUpComDO>().eq("competition_type",type));
        if(doList.isEmpty()){
            Result.fail("没有理论二次加密信息");
        }
        List<DecryptDO> dos = decryptService.selectList(new EntityWrapper<DecryptDO>().eq("field",field));
        if(!dos.isEmpty() && dos.size()==doList.size()){
            return Result.ok();
        }

        for (StuSignUpComDO stuSignUpComDO : doList) {
            boolean flag = false;
            for (DecryptDO aDo : dos) {
                if(stuSignUpComDO.getNum2().equals(aDo.getNum2())){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                DecryptDO decryptDO = new DecryptDO();
                decryptDO.setNum2(stuSignUpComDO.getNum2());
                decryptDO.setStatus(2);
                decryptDO.setField(field);
                decryptDOS.add(decryptDO);
            }
        }
        if(null!=decryptDOS && !decryptDOS.isEmpty()){
            decryptService.insertBatch(decryptDOS);
        }
        return Result.ok();
    }



    @ApiOperation(value = "根据真实考生id修改考生一次二次加密", notes = "根据真实考生id修改考生一次二次加密")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考生id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "num1s", value = "一次加密", required = true, dataType = "String"),
            @ApiImplicitParam(name = "field", value = "赛场", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/updateEncipher")
    public Result<Object> updateEncipher(Long id,String num1s,String field,boolean oneself){
        return decryptService.updateEncipher(id,num1s, field,oneself);
    }



    @ApiOperation(value = "一键全局解密", notes = "一键全局解密")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/updateStuDecrypt")
    public Result<Object> updateStuDecrypt(String username,String password,String username2,String password2){
        UserDO user = this.getUser();
        return decryptService.updateStuDecrypt(username,password,username2,password2);
    }


    /**
     * 成绩总解密列表
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/stuDecryptList")
    public Result<Page<OverallStatisticsVo>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){

        return Result.ok(examinationResultsService.overallStatistics(examinationAnswerSheetComDTO));
    }


    /**
     * 成绩公式表导出
     *
     * @param response
     */
    @ApiOperation(value = "成绩公式表导出", notes = "成绩公式表导出")
    @GetMapping("/documentWord")
    @ResponseBody
    public void documentWord(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) throws Exception {

        examinationResultsService.documentWord(response,examinationAnswerSheetComDT);
    }

    /**
     * 成绩总统计导出
     *
     * @param response
     */
    @ApiOperation(value = "成绩总统计导出", notes = "成绩总统计导出")
    @GetMapping("/downloadImportStu")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {

        examinationResultsService.overallStatisticsDownloadImport(response,examinationAnswerSheetComDT);
    }


    @ApiOperation(value = "查询没有录分的二次加密号", notes = "查询没有录分的二次加密号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decryptDO", value = "解密对象", required = true, dataType = "DecryptDO"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getNum2Init")
    public Result<Object> getNum2Init(@RequestBody DecryptDO decryptDO){
        return decryptService.getNum2Init(decryptDO);
    }


    @ApiOperation(value = "录分绑定重置", notes = "录分绑定重置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "field", value = "赛场", required = true, dataType = "String"),
            @ApiImplicitParam(name = "unitName", value = "参赛单位", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/resetDecrypt")
    public Result<?> resetDecrypt(String field,String unitName){

        return decryptService.resetDecrypt(field,unitName);
    }

    @ApiOperation(value = "一次二次加密绑定重置", notes = "一次二次加密绑定重置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "field", value = "赛场", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功",response = DecryptDO.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/resetNum1BindNum2Decrypt")
    public Result<?> resetNum1BindNum2Decrypt(String field,Long id){

        return decryptService.resetNum1BindNum2Decrypt(field,id);
    }

}
